﻿using System;
using System.Configuration;
using System.Web;
using System.Web.Mvc;
using MSAcademic.v2.Infrastructure.Picture;
using MSAcademic.v2.Infrastructure.Utility;
using MSAcademic.v2.Web.Areas.Admin.Models.Gallery;
using MSAcademic.v2.Infrastructure.Gallery;

namespace MSAcademic.v2.Web.Areas.Admin.Controllers
{
    
    public partial class GalleryController
    {
        [HttpGet]
        public ActionResult ShowGallery(int galleryId)
        {
            var viewModel = _galleryViewModelFactory();

            if (!viewModel.CanView) { return RedirectToRoute("Index"); }

            viewModel.GalleryId = galleryId;

            ViewData["CurrentView"] = "Galleries";
            return View(viewModel);
        }

        [HttpGet]
        public ActionResult ShowAllGalleries()
        {
            var viewModel = _showAllGalleriesViewModelFactory();

            if (!viewModel.CanView) { return RedirectToRoute("Index"); }

            ViewData["CurrentView"] = "Galleries";
            return View(viewModel);
        }

        [HttpGet]
        public ActionResult EditGallery(int galleryId)
        {
            var viewModel = _editGalleryViewModelFactory();
            viewModel.GalleryId = galleryId;

            if (!viewModel.CanUpdate) { return RedirectToRoute("Index"); }
            
            viewModel.GalleryName = viewModel.Gallery.Name;
            viewModel.GalleryIsHidden = viewModel.Gallery.IsHidden;

            ViewData["CurrentView"] = "Galleries";
            return View(viewModel);

        }
        [HttpPost]
        public ActionResult EditGallery(EditGalleryViewModel viewModel)
        {
            if (!viewModel.CanUpdate) { return RedirectToRoute("Index"); }

            _iGalleryCommands.Edit(viewModel.GalleryId, viewModel.GalleryName,viewModel.GalleryIsHidden);

            ViewData["CurrentView"] = "Galleries";
            return RedirectToRoute("ShowGallery", new { galleryId = viewModel.GalleryId });
        }
    }
   
    public partial class GalleryController
    {
        [HttpPost]
        public ActionResult UploadPicturesToGallery(HttpPostedFileBase pic, UploadPicturesToGalleryViewModel viewModel)
        {
            
                if (!pic.ContentType.Contains("image"))
                {
                    return Redirect(Request.UrlReferrer.ToString());
                }

                var url = _iPictureService.GalleryPictureUpload(pic, viewModel.GalleryId);
                _iPictureCommands.AddPictureToGallery(url, viewModel.GalleryId, false, "");
            

            return RedirectToRoute("ShowGallery", new { galleryId = viewModel.GalleryId });
        }
        
        [HttpPost]
        public ActionResult DownloadAndUploadPicturesToGallery(EditGalleryViewModel viewModel)
        {
            if(!string.IsNullOrEmpty(viewModel.Url) && _urlValidation.IsUrlValid(viewModel.Url))
            {
                var path=_iPictureService.DownloadAndUploadPictureToGallery(viewModel.Url, viewModel.GalleryId);
                if(path!="-1")
                {
                    _iPictureCommands.AddPictureToGallery(path,viewModel.GalleryId,false,"");
                    return RedirectToRoute("ShowGallery", new { galleryId=viewModel.GalleryId });
                }
            }
            viewModel.ErrorMessage = ConfigurationManager.AppSettings["BadUrlInputForPictureDownload"];
               
            return View("EditGallery",viewModel);

        }
    }
    public partial class GalleryController : Controller
    {
        private readonly IGalleryCommands _iGalleryCommands;
        private readonly IPictureService _iPictureService;
        private readonly IPictureCommands _iPictureCommands;
        private readonly Text _urlValidation;
        private readonly Func<GalleryViewModel> _galleryViewModelFactory;
        private readonly Func<UploadPicturesToGalleryViewModel> _uploadPicturesToGalleryViewModelFactory;
        private readonly Func<ShowAllGalleriesViewModel> _showAllGalleriesViewModelFactory;
        private readonly Func<EditGalleryViewModel> _editGalleryViewModelFactory;
        private readonly Func<DownloadAndUploadPicturesToGalVIewModel> _downloadAnduploadViewModelFactory;
  

        public GalleryController(IPictureCommands iPictureCommands,
            IPictureService iFileSystemPictureService,
            IGalleryCommands iGalleryCommands, 
            Func<EditGalleryViewModel> editGalleryViewModelFactory,
            Func<GalleryViewModel> galleryViewModelFactory,
            Func<ShowAllGalleriesViewModel> showAllGalleriesViewModelFactory,
            Func<DownloadAndUploadPicturesToGalVIewModel> downloadAnduploadViewModelFactory,
            Func<UploadPicturesToGalleryViewModel> uploadPicturesToGalleryViewModelFactory,
            Text urlValidation)
        {
            _galleryViewModelFactory = galleryViewModelFactory;
     
            _showAllGalleriesViewModelFactory = showAllGalleriesViewModelFactory;
            _editGalleryViewModelFactory = editGalleryViewModelFactory;
            _downloadAnduploadViewModelFactory = downloadAnduploadViewModelFactory;
            _iGalleryCommands = iGalleryCommands;
            _iPictureService = iFileSystemPictureService;
            _iPictureCommands = iPictureCommands;
            _urlValidation = urlValidation;
            _uploadPicturesToGalleryViewModelFactory = uploadPicturesToGalleryViewModelFactory;

        }

    }


}
